<template>
    <el-form :model="conditionForm"
             ref="conditionForm"
             :label-width="labelWidth"
             class="inline-form">
        <el-form-item label="永久免费参与次数"
                      prop="freeParticipationNum"
                      :rules="[
                      { required: true, message: '不能为空'},
                      { type: 'number', message: '必须为数字值'}
    ]">
            <el-input v-model.number="conditionForm.freeParticipationNum"
                      :disabled="guessFlag"
                      type="freeParticipationNum"></el-input>
        </el-form-item>
        <el-form-item label="积分参与有效次数"
                      prop="integralParticipationTime"
                      :rules="[
                        { required: true, message: '不能为空'},
                         { type: 'number', message: '必须为数字值'}
    ]">
            <el-input v-model.number="conditionForm.integralParticipationTime"
                      type="integralParticipationTime"></el-input>
        </el-form-item>
        <el-form-item label="参与总次数"
                      prop="">
            <el-input v-model.number="conditionForm.maxNum"
                      :disabled="true">{{sum}}}
            </el-input>
        </el-form-item>
        <el-form-item label="是否每日刷新">
            <el-switch v-model="conditionForm.refresh"></el-switch>
        </el-form-item>
        <el-form-item label="每日刷新题目数量"
                      prop="countNum"
                      v-if="json.templateMark==='daily'"
                      :rules="[
                        { required: true, message: '不能为空'},
                         { type: 'number', message: '必须为数字值'}
    ]">
            <el-input v-model.number="conditionForm.countNum"
                      type="countNum"></el-input>
        </el-form-item>
        <el-form-item label="是否显示用户留言">
            <el-switch v-model="conditionForm.saveMessage"></el-switch>
        </el-form-item>
        <el-form-item label="是否显示用户留言展示">
            <el-switch v-model="conditionForm.showMessage"></el-switch>
        </el-form-item>
        <el-form-item label="积分配置">
            <el-switch v-model="conditionForm.limitScore"
                       @change="setChangeHandle"></el-switch>
        </el-form-item>
        <div v-if="conditionForm.limitScore">
            <el-form-item>
                <div v-if="doll">
                    <el-radio-group name="prizeClawType"
                                    v-model="conditionForm.prizeClawType"
                                    @change="getPrizeClawConditionInfo">
                        <el-radio class="radio" label="low">低配</el-radio>
                        <el-radio class="radio" label="high">高配</el-radio>
                    </el-radio-group>
                </div>
            </el-form-item>
            <el-form-item label="单次消耗积分" prop="consumeIntegral">
                <el-input v-model.number="conditionForm.consumeIntegral"></el-input>
            </el-form-item>
            <el-form-item label="来源Id" prop="resourceId">
                <el-input v-model="conditionForm.resourceId"></el-input>
            </el-form-item>
            <el-form-item label="来源描述">
                <el-input v-model="conditionForm.resourceDescription"></el-input>
            </el-form-item>
            <el-form-item label="渠道Id" prop="channelId">
                <el-input v-model="conditionForm.channelId"></el-input>
            </el-form-item>
            <el-form-item label="渠道密钥">
                <el-input v-model="conditionForm.channelSecretkey"></el-input>
            </el-form-item>
            <el-form-item label="渠道描述">
                <el-input v-model="conditionForm.channelDescription"></el-input>
            </el-form-item>
        </div>
        <el-form-item>
            <el-button class="pan-btn green-btn sm-btn"
                       size="small"
                       @click="saveConditionHandle">保存
            </el-button>
        </el-form-item>
    </el-form>
</template>
<script>
    import {parseTime} from '@/utils/index'
    import request from '@/utils/request'

    export default {
        name: 'condition',
        props: ['MsgEditData', 'json'],
        data() {
            return {
                doll: false,
                labelWidth: '160px',
                editFlag: '',
                PrizeClawType: 'low',
                guessFlag: false,
                conditionForm: {
                    prizeClawType: '',
                    // /////条件设置
                    freeParticipationNum: 1,
                    integralParticipationTime: 1,
                    maxNum: 1,
                    countNum: '',
                    refresh: false,
                    limitScore: false,
                    channelDescription: '',
                    channelSecretkey: '',
                    channelId: '',
                    resourceId: '',
                    resourceDescription: '',
                    consumeIntegral: 0,
                    saveMessage: false,
                    showMessage: false
                }

            }
        },
        // 加一块等于第三个的值
        computed: {
            sum: function () {
                this.conditionForm.maxNum = parseInt(this.conditionForm.freeParticipationNum) + parseInt(this.conditionForm.integralParticipationTime)
            }
        },
        mounted() {
            if (this.json.templateMark === 'prizeclaw') {
                this.doll = true
            } else {
                this.doll = false
            }
            console.log(this.json)
            this.$nextTick(function () {
                if (this.json.activityId != undefined && this.json.pageModel == 'condtion') {
                    if (this.json.templateMark === 'prizeclaw') {
                        this.getPrizeClawConditionInfo();
                    } else {
                        this.getConditionInfo();
                    }
                }
                this.guessFlag = (this.json.templateMark === 'guess' || this.json.templateMark === 'tournamentguess') ? true : false;
                this.conditionForm.freeParticipationNum = (this.json.templateMark === 'guess' || this.json.templateMark === 'tournamentguess') ? 0 : 1;
            })
        },
        methods: {
            setChangeHandle(val) {
                if (!val) {
                    this.conditionForm.consumeIntegral = 0
                    this.conditionForm.channelId = ''
                    this.conditionForm.resourceId = ''
                    this.conditionForm.resourceDescription = ''
                    this.conditionForm.channelDescription = ''
                    this.conditionForm.channelSecretkey = ''
                }
            },
            getPrizeClawConditionInfo(value) {
                console.log("prizeClaw")
                const _this = this
                if (value != null) {
                    _this.PrizeClawType = value;
                }
                // 获取抓娃娃条件设置
                request({
                    url: '/activityConsole/activity/activityInfoId/' + _this.json.activityId + '/rule/findByOne',
                    method: 'get',
                    params: {
                        PrizeClawType: _this.PrizeClawType
                    },
                }).then(res => {
                    console.log(res)
                    if (res.data != null) {
                        // const dataObj = JSON.parse(JSON.stringify(res.data))
                        _this.conditionForm = res.data
                        _this.conditionForm.createDate = parseTime(_this.conditionForm.createDate)
                    }
                }).catch(err => {
                    console.log(err)
                })

            },
            getConditionInfo() {
                // 获取条件设置
                const _this = this
                request({
                    url: '/activityConsole/activity/activityInfoId/' + _this.json.activityId + '/rule',
                    method: 'get'
                }).then(res => {
                    console.log(res)
                    if (res.data != null) {
                       // const dataObj = JSON.parse(JSON.stringify(res.data))
                        _this.conditionForm = res.data
                        _this.conditionForm.freeParticipationNum = (_this.json.templateMark === 'guess' || _this.json.templateMark === 'tournamentguess') ? 0 : _this.conditionForm.freeParticipationNum;
                        _this.conditionForm.createDate = parseTime(_this.conditionForm.createDate)
                    }
                }).catch(err => {
                    console.log(err)
                })
            },
            saveConditionHandle() {
                // ////////////保存条件设置
                const _this = this;
                const params = _this.conditionForm;
                if (_this.json.templateMark == 'prizeclaw') {
                    console.log(_this.json.templateMark);
                    this.$refs.conditionForm.validate(valid => {
                            if (valid) {
                                request({
                                    url: '/activityConsole/activity/activityInfoId/' + _this.json.activityId + '/rule/savePrizeClaw',
                                    method: 'post',
                                    params: params
                                }).then(res => {
                                    console.log(res)
                                    const state = res.status == 200 ? 'success' : 'error'
                                    _this.$message({
                                        type: state,
                                        message: res.description
                                    })
                                }).catch(err => {
                                    console.log(err)
                                })
                            }
                        }
                    )
                } else {
                    this.$refs.conditionForm.validate(valid => {
                            if (valid) {
                                request({
                                    url: '/activityConsole/activity/activityInfoId/' + _this.json.activityId + '/rule',
                                    method: 'post',
                                    params: params
                                }).then(res => {
                                    console.log(res)
                                    // if (res.status == 200) {
                                    //     _this.$emit('listenMsgEdit', {
                                    //         hideSetFlag: true,
                                    //
                                    //         pageModel: 'edit',
                                    //     });
                                    // }
                                    const state = res.status == 200 ? 'success' : 'error'
                                    _this.$message({
                                        type: state,
                                        message: res.description
                                    })
                                }).catch(err => {
                                    console.log(err)
                                })
                            }
                        }
                    )
                }
            }
        }
    }
</script>
